/**
 * Copyright (C) 2010 Cloudfarming <info@cloudfarming.nl>
 *
 * Licensed under the Eclipse Public License - v 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.eclipse.org/legal/epl-v10.html
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package nl.cloudfarming.client.geoviewer.jxmap.layerlist;

import java.awt.event.ItemEvent;
import javax.swing.JSlider;
import nl.cloudfarming.client.geoviewer.Layer;
import nl.cloudfarming.client.geoviewer.MapController;

/**
 * Panel for showing (and editing) layer settings, like visibility and transparency.
 * 
 * @author Timon Veenstra
 */
public class LayerSettingsPanel extends javax.swing.JPanel {

    private MapController mapApi;
    private Layer layer;
    private boolean isVisibleInit;
    private boolean initDialogFirstTime = true;
    private int transparancyInit;

    /** Creates new form LayerSettingsPanel */
    public LayerSettingsPanel(MapController mapApi, Layer layer) {
        this.mapApi = mapApi;
        this.layer = layer;
        initComponents();
        initSettings();
    }

    /**
     * Initialize the settings in het panel based on the layer info.
     */
    private void initSettings() {
        isVisibleInit = mapApi.getLayerInfo(layer).isVisible();
        transparancyInit = recalcTrans(mapApi.getLayerInfo(layer).getTransparency());

        String displayName = layer.getName();
        if (displayName == null) {
            displayName = layer.getName();
        }

        jLabelTitleText.setText(displayName);
//        jLabelServiceText.setText(layer.getUrl());

        jCheckBoxVisibility.setSelected(isVisibleInit);
        jSliderTransparancy.setValue(transparancyInit);
    }

    /**
     * Cancel the changes, so go back to the settings while the panel opened.
     */
    public void cancelSettings() {
        if (isVisibleInit) {
             mapApi.getLayerInfo(layer).setVisible(true);
        } else {
            mapApi.getLayerInfo(layer).setVisible(false);
        }
        mapApi.getLayerInfo(layer).setTransparency(recalcTrans(transparancyInit));
    }

    private float recalcTrans(int trans){
        return (float)((float)(100-trans)/100.0);
    }

    private int recalcTrans(float trans){
        return (int)trans*100;
    }


    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanelSettings = new javax.swing.JPanel();
        jLabelTitle = new javax.swing.JLabel();
        jLabelTitleText = new javax.swing.JLabel();
        jLabelService = new javax.swing.JLabel();
        jLabelServiceText = new javax.swing.JLabel();
        jLabelVisibility = new javax.swing.JLabel();
        jLabelTransparancy = new javax.swing.JLabel();
        jSliderTransparancy = new javax.swing.JSlider();
        jCheckBoxVisibility = new javax.swing.JCheckBox();

        jPanelSettings.setBorder(javax.swing.BorderFactory.createTitledBorder(org.openide.util.NbBundle.getMessage(LayerSettingsPanel.class, "LayerSettingsPanel.jPanelSettings.border.title"))); // NOI18N
        jPanelSettings.setName(""); // NOI18N

        jLabelTitle.setText(org.openide.util.NbBundle.getMessage(LayerSettingsPanel.class, "LayerSettingsPanel.jLabelTitle.text")); // NOI18N

        jLabelTitleText.setFont(new java.awt.Font("Dialog", 0, 12));
        jLabelTitleText.setText(org.openide.util.NbBundle.getMessage(LayerSettingsPanel.class, "LayerSettingsPanel.jLabelTitleText.text")); // NOI18N

        jLabelService.setText(org.openide.util.NbBundle.getMessage(LayerSettingsPanel.class, "LayerSettingsPanel.jLabelService.text")); // NOI18N

        jLabelServiceText.setFont(new java.awt.Font("Dialog", 0, 12));
        jLabelServiceText.setText(org.openide.util.NbBundle.getMessage(LayerSettingsPanel.class, "LayerSettingsPanel.jLabelServiceText.text")); // NOI18N

        jLabelVisibility.setText(org.openide.util.NbBundle.getMessage(LayerSettingsPanel.class, "LayerSettingsPanel.jLabelVisibility.text")); // NOI18N

        jLabelTransparancy.setText(org.openide.util.NbBundle.getMessage(LayerSettingsPanel.class, "LayerSettingsPanel.jLabelTransparancy.text")); // NOI18N

        jSliderTransparancy.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                jSliderTransparancyStateChanged(evt);
            }
        });

        jCheckBoxVisibility.setText(org.openide.util.NbBundle.getMessage(LayerSettingsPanel.class, "LayerSettingsPanel.jCheckBoxVisibility.text")); // NOI18N
        jCheckBoxVisibility.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jCheckBoxVisibilityItemStateChanged(evt);
            }
        });

        javax.swing.GroupLayout jPanelSettingsLayout = new javax.swing.GroupLayout(jPanelSettings);
        jPanelSettings.setLayout(jPanelSettingsLayout);
        jPanelSettingsLayout.setHorizontalGroup(
            jPanelSettingsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelSettingsLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelSettingsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanelSettingsLayout.createSequentialGroup()
                        .addComponent(jLabelTransparancy, javax.swing.GroupLayout.DEFAULT_SIZE, 254, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                    .addGroup(jPanelSettingsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(jLabelVisibility, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabelService, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabelTitle, javax.swing.GroupLayout.DEFAULT_SIZE, 109, Short.MAX_VALUE)))
                .addGroup(jPanelSettingsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jSliderTransparancy, javax.swing.GroupLayout.PREFERRED_SIZE, 295, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jCheckBoxVisibility, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanelSettingsLayout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanelSettingsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jLabelServiceText, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabelTitleText, javax.swing.GroupLayout.DEFAULT_SIZE, 421, Short.MAX_VALUE))))
                .addContainerGap())
        );
        jPanelSettingsLayout.setVerticalGroup(
            jPanelSettingsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelSettingsLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelSettingsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabelTitle)
                    .addComponent(jLabelTitleText, javax.swing.GroupLayout.PREFERRED_SIZE, 15, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanelSettingsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabelService)
                    .addComponent(jLabelServiceText, javax.swing.GroupLayout.PREFERRED_SIZE, 15, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(71, 71, 71)
                .addGroup(jPanelSettingsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabelVisibility)
                    .addComponent(jCheckBoxVisibility))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanelSettingsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabelTransparancy)
                    .addComponent(jSliderTransparancy, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(36, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanelSettings, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanelSettings, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanelSettings.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(LayerSettingsPanel.class, "LayerSettingsPanel.jPanel1.AccessibleContext.accessibleName")); // NOI18N
    }// </editor-fold>//GEN-END:initComponents

private void jSliderTransparancyStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_jSliderTransparancyStateChanged
    JSlider source = (JSlider) evt.getSource();
    mapApi.getLayerInfo(layer).setTransparency(recalcTrans(source.getValue()));
}//GEN-LAST:event_jSliderTransparancyStateChanged

private void jCheckBoxVisibilityItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jCheckBoxVisibilityItemStateChanged
    if (initDialogFirstTime) {
        initDialogFirstTime = false;
    } else {
        if (evt.getStateChange() == ItemEvent.SELECTED) {
            mapApi.getLayerInfo(layer).setVisible(true);
        } else {
            mapApi.getLayerInfo(layer).setVisible(false);
        }
    }
}//GEN-LAST:event_jCheckBoxVisibilityItemStateChanged
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox jCheckBoxVisibility;
    private javax.swing.JLabel jLabelService;
    private javax.swing.JLabel jLabelServiceText;
    private javax.swing.JLabel jLabelTitle;
    private javax.swing.JLabel jLabelTitleText;
    private javax.swing.JLabel jLabelTransparancy;
    private javax.swing.JLabel jLabelVisibility;
    private javax.swing.JPanel jPanelSettings;
    private javax.swing.JSlider jSliderTransparancy;
    // End of variables declaration//GEN-END:variables
}
